using GHIElectronics.NETMF.Glide.UI;

namespace Melody_Glider.GUIContent
{
    class NameInputWindow: WindowFrame
    {
        private readonly GUI _gui;
        private readonly SystemState _conditions;
        private bool _shift;
        private string _tempFileName;
        private readonly string[] _buttonNames = new[]
                                                 {
                                                     "Button_1~", "Button_2!", "Button_3(", "Button_4)", "Button_5[", "Button_6]", 
                                                     "Button_7+", "Button_8-", "Button_9=", "Button_0_", "Button_qQ", "Button_wW", 
                                                     "Button_eE", "Button_rR", "Button_tT", "Button_yY", "Button_uU", "Button_iI", 
                                                     "Button_oO", "Button_pP", "Button_aA", "Button_sS", "Button_dD", "Button_fF", 
                                                     "Button_gG", "Button_hH", "Button_jJ", "Button_kK", "Button_lL", "Button_zZ", 
                                                     "Button_xX", "Button_cC", "Button_vV", "Button_bB", "Button_nN", "Button_mM", 
                                                     "Button_.,"
                                                 };

        public NameInputWindow(GUI gui, SystemState state)
        {
            _gui = gui;
            _conditions = state;
        }

        private void Update()
        {
            var nameText = (TextBox)ThisWindow.GetChildByName("NameBox");
            nameText.Text = _tempFileName;
            nameText.Invalidate();
            if (_tempFileName.Length != 0)
                _gui.EnableButton("OKButton", ThisWindow);
            if (_tempFileName.Length == 0)
                _gui.DisableButton("OKButton", ThisWindow);
        }

        protected override void InitButtons()
        {
            for (int i = 0; i < 37; i++)
            {
                var btn = (Button)ThisWindow.GetChildByName(_buttonNames[i]);
                btn.TapEvent += BtnTapEvent;
            }

            var btnShift = (Button)ThisWindow.GetChildByName("Button_Shift");
            btnShift.TapEvent += BtnShiftTapEvent;

            var btnClear = (Button)ThisWindow.GetChildByName("Button_Clear");
            btnClear.TapEvent += BtnClearTapEvent;

            var btnSpace = (Button)ThisWindow.GetChildByName("Button_Space");
            btnSpace.TapEvent += BtnSpaceTapEvent;

            var btnBksp = (Button)ThisWindow.GetChildByName("Button_Backspace");
            btnBksp.TapEvent += BtnBkspTapEvent;

            var btnOk = (Button)ThisWindow.GetChildByName("OKButton");
            btnOk.TapEvent += BtnOkTapEvent;

            var btnCancel = (Button)ThisWindow.GetChildByName("CancelButton");
            btnCancel.TapEvent += BtnCancelTapEvent;
        }

        private void BtnTapEvent(object sender)
        {
            var tappedBtn = (Button)sender;
            var tappedContent = tappedBtn.Name.Substring(7);

            if (_tempFileName.Length == 16)
                return;

            if (_shift)
                _tempFileName += tappedContent[1];
            else
                _tempFileName += tappedContent[0];

            Update();
        }

        private void BtnOkTapEvent(object sender)
        {
            _conditions.ActiveFileName = _tempFileName;
            _conditions.NewName = false;
            _conditions.IsFileSaved = false;

            _gui.EditorWindow.SetToMain();
        }

        private void BtnCancelTapEvent(object sender)
        {
            _gui.EditorWindow.SetToMain();
        }

        private void BtnBkspTapEvent(object sender)
        {
            if (_tempFileName.Length == 0)
                return;

            _tempFileName = _tempFileName.Substring(0, _tempFileName.Length - 1);

            Update();
        }

        private void BtnSpaceTapEvent(object sender)
        {
            if (_tempFileName.Length == 16)
                return;

            _tempFileName += " ";

            Update();
        }

        private void BtnClearTapEvent(object sender)
        {
            if (_tempFileName.Length == 0)
                return;

            _tempFileName = "";

            Update();
        }

        private void BtnShiftTapEvent(object sender)
        {
            var btnShift = (Button)ThisWindow.GetChildByName("Button_Shift");

            if (_shift)
            {
                btnShift.Text = "SHIFT";
                btnShift.Invalidate();

                _shift = false;
            }
            else
            {
                btnShift.Text = "shift";
                btnShift.Invalidate();

                _shift = true;
            }
        }

        protected override void PrepareToMain()
        {
            var nameText = (TextBox)ThisWindow.GetChildByName("NameBox");

            if (_conditions.NewName)
            {
                nameText.Text = "";
                _tempFileName = "";
            }
            else
            {
                nameText.Text = _conditions.ActiveFileName;
                _tempFileName = _conditions.ActiveFileName;
            }

            nameText.Invalidate();

            if (_tempFileName.Length == 0)
                _gui.DisableButton("OKButton", ThisWindow);

            _shift = false;
        }
    }
}
